//
//  MeViewController.m
//  KeepAccounts
//
//  Created by honeyrr on 2017/10/1.
//  Copyright © 2017年 honeyrr. All rights reserved.
//

#import "KAMeViewController.h"
#import "KAMeHeaderView.h"
#import "KAMessageViewController.h"
#import "KAPlanViewController.h"
#import "KAStatisticsViewController.h"
#import "KAAboutViewController.h"
#import "KASatisfactionViewController.h"
#import <ShareSDK/ShareSDK.h>
#import <ShareSDKUI/ShareSDK+SSUI.h>
#import "KALoginVC.h"

@interface KAMeViewController ()<UITableViewDelegate, UITableViewDataSource,KAMeHeaderViewDelegate>

@property(nonatomic, strong) UITableView *myTableView;

@property(nonatomic, strong) KAMeHeaderView *headerView;

@property(nonatomic, strong) NSArray *tableArr;
@end

@implementation KAMeViewController

-(NSArray *)tableArr
{
    if (!_tableArr) {
        _tableArr = [NSArray array];
    }
    return _tableArr;
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:YES];
    [self.view addSubview:self.myTableView];
    self.headerView.name = [[NSUserDefaults standardUserDefaults] valueForKey:@"userName"];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setUpUI];
}

-(void) setUpUI
{
    self.tableArr = @[@{@"资产计划":@"me01"}, @{@"清除数据":@"me02"}, @{@"用户满意度":@"me03"}, @{@"分享有礼":@"me04"}, @{@"关于我们":@"me05"}];
    [self.myTableView reloadData];
}

-(KAMeHeaderView *)headerView
{
    if (!_headerView) {
        KAMeHeaderView *headerView = [[KAMeHeaderView alloc]initWithFrame:CGRectMake(0, 0, KAScreenW, 200)];
        headerView.delegate = self;
        _headerView = headerView;
    }
    return _headerView;
}

-(void)headerIconBtnClick:(UILabel *)headerUserLabel
{
    if ([[[NSUserDefaults standardUserDefaults] valueForKey:@"userName"] isEqualToString:@"请登录"]) {
        [self.navigationController pushViewController:[KALoginVC new] animated:YES];
    } else {
        UIAlertController *alert =[UIAlertController alertControllerWithTitle:[NSString stringWithFormat:@"您已登录：%@",[[NSUserDefaults standardUserDefaults] valueForKey:@"userName"]] message:@"" preferredStyle:UIAlertControllerStyleAlert];
        [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]];
        [alert addAction:[UIAlertAction actionWithTitle:@"注销" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
            [self.navigationController pushViewController:[KALoginVC new] animated:YES];
            [[NSUserDefaults standardUserDefaults] setObject:@"请登录" forKey:@"userName"];
        }]];
        [self presentViewController:alert animated:YES completion:nil];
    }
    
}

-(void)headerMsgBtnClick:(KAMeHeaderView *)headerView
{
    KAMessageViewController *msgVC = [KAMessageViewController new];
    [self.navigationController pushViewController:msgVC animated:YES];
}

-(UITableView *)myTableView
{
    if (!_myTableView) {
        UITableView *tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, -20, KAScreenW, KAScreenH) style:UITableViewStylePlain];
        tableView.delegate = self;
        tableView.dataSource = self;
        tableView.tableHeaderView = self.headerView;
        tableView.tableFooterView = [UIView new];
        _myTableView = tableView;
    }
    return _myTableView;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.tableArr.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"meCell"];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"meCell"];
    }
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    NSString *key = [[self.tableArr[indexPath.row] allKeys] firstObject];
    cell.textLabel.text = key;
    cell.imageView.image = [UIImage imageNamed:[self.tableArr[indexPath.row] valueForKey:key]];
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 64;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    switch (indexPath.row) {
        case 0:
            [self.navigationController pushViewController:[KAPlanViewController new] animated:YES];
            break;
        case 1:
            [self clearData];
            break;
        case 2:
            [self.navigationController pushViewController:[KASatisfactionViewController new] animated:YES];
            break;
        case 3:
            [self sharedBtnClick];
            break;
        case 4:
            [self.navigationController pushViewController:[KAAboutViewController new] animated:YES];
            break;
            
        default:
            break;
    }
    [self.myTableView deselectRowAtIndexPath:indexPath animated:YES];
}

-(void) clearData
{
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"您确认要清除所有数据吗？" preferredStyle:UIAlertControllerStyleAlert];
    [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]];
    [alert addAction:[UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
        [[KADataBase instance] clearDataBase];
    }]];
    [self presentViewController:alert animated:YES completion:nil];
}

-(void)sharedBtnClick
{
    //1、创建分享参数
    NSArray* imageArray = @[[UIImage imageNamed:@"icon.png"]];
    if (imageArray) {
        NSMutableDictionary *shareParams = [NSMutableDictionary dictionary];
        NSString *paramText = @"轻生活！简记账！ 欢迎来到记账平台，不管你是北漂族、学生党还是高级白领，都适合你的独家定制账本.不管你身在何处，动动手指3秒钟快速记账;想要查询历史账单？只需一键滑动，每月支出收入报表一目了然,帮助您合理分析资金流向。 我们不做千篇一律的数据搬运，只做您最贴心的记账管家！";
        [shareParams SSDKSetupShareParamsByText:paramText images:imageArray url:[NSURL URLWithString:@"http://www.mob.com"] title:@"开心记账" type:SSDKContentTypeAuto];
        //有的平台要客户端分享需要加此方法，例如微博
        [shareParams SSDKEnableUseClientShare];
        //2、分享（可以弹出我们的分享菜单和编辑界面）
        //要显示菜单的视图, iPad版中此参数作为弹出菜单的参照视图，只有传这个才可以弹出我们的分享菜单，可以传分享的按钮对象或者自己创建小的view 对象，iPhone可以传nil不会影响
        [ShareSDK showShareActionSheet:nil items:nil shareParams:shareParams onShareStateChanged:^(SSDKResponseState state, SSDKPlatformType platformType, NSDictionary *userData, SSDKContentEntity *contentEntity, NSError *error, BOOL end){
            switch (state) {
                case SSDKResponseStateSuccess: {
                    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"分享成功" message:nil delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
                    [alertView show];
                    break;
                }
                case SSDKResponseStateFail: {
                    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"分享失败" message:[NSString stringWithFormat:@"%@",error] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
                    [alert show];
                    break;
                }
                default:
                    break;
            }
        }];
    }
}
@end
